// gorpc/codec/codec.go

package codec

import "gorpc/common"

type CodecType byte

const (
	ProtoBuffer CodecType = CodecType(iota)
	JSON
)

type Codec interface {
	Encode(interface{}) ([]byte, error)
	Decode([]byte, interface{}) error
}

// NewCodec 编解码器工厂函数
func NewCodec(t CodecType) (Codec, error) {
	switch t {
	case ProtoBuffer:
		return &ProtobufCodec{}, nil
	case JSON:
		return &JSONCodec{}, nil
	default:
		return nil, common.NewRPCError(
			common.ErrorCodeSerialization,
			"unsupported codec type",
			nil)
	}
}
